<?php

/**
 * This is the model class for table "{{nav}}".
 *
 * The followings are the available columns in table '{{nav}}':
 * @property integer $zna_id
 * @property integer $zna_type
 * @property string $zna_title
 * @property string $zna_seotitle
 * @property string $zna_seokey
 * @property string $zna_seodesc
 * @property string $zna_url
 * @property string $zna_target
 * @property string $zna_desc
 * @property integer $zna_highlight
 * @property string $zna__childid
 * @property integer $zna_parentid
 * @property integer $zna_topid
 * @property integer $zna_listorder
 * @property integer $zna_status
 * @property integer $inputtime
 * @property integer $updatetime
 * 张梦林
 * 2012-12-5
 */
class Nav extends CActiveRecord
{
	/**
	 * Returns the static model of the specified AR class.
	 * @param string $className active record class name.
	 * @return Nav the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return '{{nav}}';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('zna_type, zna_ifposition, zna_highlight, zna_parentid, zna_topid, zna_listorder, zna_status, inputtime, updatetime, zna_pagetype', 'numerical', 'integerOnly'=>true),
			array('zna_title', 'length', 'max'=>25),
			array('zna_seotitle, zna_seokey', 'length', 'max'=>100),
			array('zna_seodesc, zna_desc', 'length', 'max'=>255),
			array('zna_url', 'length', 'max'=>50),
			array('zna_target', 'length', 'max'=>6),
			array('zna__childid', 'safe'),
			// The following rule is used by search().
			// Please remove those attributes that should not be searched.
			array('zna_id, zna_type, zna_title, zna_seotitle, zna_seokey, zna_seodesc, zna_url, zna_target, zna_desc, zna_highlight, zna__childid, zna_parentid, zna_topid, zna_listorder, zna_status, inputtime, updatetime', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
				
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'zna_id' => '菜单ID',
			'zna_type' => '类型',
			'zna_pagetype' => '列表类型',
			'zna_title' => '菜单名',
			'zna_seotitle' => 'SEO标题',
			'zna_seokey' => 'SEO',		// 关键字，多个用逗号隔开
			'zna_seodesc' => 'SEO描述',
			'zna_url' => '栏目地址',
			'zna_target' => '跳转方式',
			'zna_desc' => '备注',
			'zna_highlight' => '是否高亮', //1否2是
			'zna__childid' => '子ID',	//子ID，多个用逗号分隔开
			'zna_parentid' => '上组ID',
			'zna_topid' => '顶级ID',
			'zna_listorder' => '排序',
			'zna_status' => '状态', //1显示，2不显示
			'inputtime' => '录入时间',
			'updatetime' => '更新时间',
			'zna_ifposition' => '是否推荐位',
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search($page=10)
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('zna_id',$this->zna_id);
		$criteria->compare('zna_type',$this->zna_type);
		$criteria->compare('zna_pagetype',$this->zna_pagetype);
		$criteria->compare('zna_title',$this->zna_title,true);
		$criteria->compare('zna_seotitle',$this->zna_seotitle,true);
		$criteria->compare('zna_seokey',$this->zna_seokey,true);
		$criteria->compare('zna_seodesc',$this->zna_seodesc,true);
		$criteria->compare('zna_url',$this->zna_url,true);
		$criteria->compare('zna_target',$this->zna_target,true);
		$criteria->compare('zna_desc',$this->zna_desc,true);
		$criteria->compare('zna_highlight',$this->zna_highlight);
		$criteria->compare('zna__childid',$this->zna__childid,true);
		$criteria->compare('zna_parentid',$this->zna_parentid);
		$criteria->compare('zna_topid',$this->zna_topid);
		$criteria->compare('zna_listorder',$this->zna_listorder);
		$criteria->compare('zna_status',$this->zna_status);
		$criteria->compare('inputtime',$this->inputtime);
		$criteria->compare('updatetime',$this->updatetime);
		//$criteria->order = ' zna_listorder ASC ';
		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
			'pagination'=>array(
				'pageSize'=>$page,
			),
		));
	}

	/**
	 * 返回推荐位页面
	 * $type 1产品 2资讯
	 * @return CActiveDataProvider 
	 */
	public function posPage($type)
	{
		$criteria=new CDbCriteria;
		$criteria->compare('zna_ifposition',array(3,$type));
		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
		));
	}

	/**
	 * 返回推荐位页面数据
	 * $type 1产品 2资讯
	 * @return CActiveDataProvider 
	 */
	public function posPageList($type)
	{
		$criteria=new CDbCriteria;
		$criteria->compare('zna_ifposition',array(3,$type));
		return $this->findAll($criteria);
	}
	/*
	 * 前台菜单类型
	* */
	public function getType($type=null){
		if($type==null){
			return array(
					'1'=>'商家中心',
					'2'=>'会员中心',
					'3'=>'前端',
			);
		}else{
			$level = $this->getType();
			return $level[$type];
		}
	}
	/*
	 * 跳转方式
	* */
	public function getTarget($type=null){
		if($type==null){
			return array(
					'_self'=>'本窗口打开',
					'_blank'=>'新窗口打开',
			);
		}else{
			$level = $this->getTarget();
			return $level[$type];
		}
	}
	/*
	 * 状态
	* */
	public function getStatus($type=null){
		if($type==null){
			return array(
					'1'=>'显示',
					'2'=>'不显示',
			);
		}else{
			$level = $this->getStatus();
			return $level[$type];
		}
	}
	
	/**
	 * zna_pagetype列表类型
	 * 0非列表 1产品列表 2资讯列表',
	 */
	public function getZnaPageType($type=null){
		if($type==null){
			return array(
				'0'=>'非列表',
				'1'=>'产品列表',
				'2'=>'资讯列表'
				);
		}else{
			$level = $this->getZnaPageType();
			if(array_key_exists($type,$level))
				return $level[$type];
		}
	}
	
	/*
	 * 获取菜单名称
	* */
	public function getTitle($type){
		$criteria=new CDbCriteria;
		$criteria->compare('zna_id',$type);
		$nav = $this->find($criteria);
		if(!empty($nav)){
			return $nav->zna_title;
		}else{
			//echo '一级菜单';
		}
	}
	/*
	 * 获取各级菜单名称，格式：“我的家猫>帐号管理>资料修改”
	* */
	public function getMenuStr($id){
		$title = '';
		$menu1 = $this->findByPk($id);
		$title = $menu1 ? $menu1->zna_title : $title;
		if($menu1 && $menu1->zna_parentid!=0){
			$menu2 = $this->findByPk($menu1->zna_parentid);
			$title = $menu2 ? $menu2->zna_title.'>'.$title : $title;
			if($menu2 && $menu2->zna_parentid!=0){
				$menu3 = $this->findByPk($menu2->zna_parentid);
				$title = $menu3 ? $menu3->zna_title.'>'.$title : $title;
			}
		}
		return $title;

	}
	/*
	 * 排序
	* */
	public function listOrder($listData){
		foreach($listData as $key=>$val){
			$model = $this->findByPk($key);
			$model->zna_listorder = $val;
			$model->save();
		}
	}
	/*
	 * 产品推荐位页面设置
	* */
	public function setIfPosition($listData,$type=1){
		if($type==1){
			$this->updateAll(array('zna_ifposition'=>'0'),'zna_ifposition=1');
			$this->updateAll(array('zna_ifposition'=>'2'),'zna_ifposition=3');
			foreach($listData as $key=>$val){
				$model = $this->findByPk($key);
				$model->zna_ifposition = $model->zna_ifposition ? 3 : $val;
				$model->save();
			}
		}else{
			$this->updateAll(array('zna_ifposition'=>'0'),'zna_ifposition=2');
			$this->updateAll(array('zna_ifposition'=>'1'),'zna_ifposition=3');
			foreach($listData as $key=>$val){
				$model = $this->findByPk($key);
				$model->zna_ifposition = $model->zna_ifposition ? 3 : 2;
				$model->save();
			}
		}
	}
	/*
	 * 取消产品推荐位页面
	* */
	public function unsetPositionPage($key,$type=1){
		$model = $this->findByPk($key);		
		if($type==1){
			$model->zna_ifposition = $model->zna_ifposition>=2 ? 2 : '0';
		}else{
			$model->zna_ifposition = $model->zna_ifposition>=2 ? $model->zna_ifposition-2 : $model->zna_ifposition;
		}
		$model->save();
	}
	/**
	 * 保存数据前做此操作
	 * */
	protected function beforeSave()
	{
		if(parent::beforeSave()){
			if($this->isNewRecord){
				$this->inputtime = time();
				$nav = $this->findByPk($this->zna_parentid);
				if(isset($nav->zna_parentid))
					$this->zna_topid = $nav->zna_parentid;
				return true;
			}else{
				$this->updatetime = time();
				$nav = $this->findByPk($this->zna_parentid);
				if(isset($nav->zna_parentid))
					$this->zna_topid = $nav->zna_parentid;
				return true;
			}		
		}else{
			return false;
		}
	}
	/*
	 * 父级菜单下垃框列表数据
	* */
	public function getParent($type=null){
		if($type==null){
			$navs = $this->findAll();
			if(empty($criteria)){
				$dataArray[0] = '一级菜单';
				$navs = simplifyData($navs);
				foreach ($navs as $arr){
					$dataArray[$arr['zna_id']] = $arr['zna_title'];
				}
				return $dataArray;
			}else{
				return false;
			}
		}else{
			$level = $this->getParent();
			return $level[$type];
		}
	}
	/*
	 * 一级菜单下垃框列表数据
	* */
	public function getTopData(){
		$criteria=new CDbCriteria;
		$criteria->compare('zna_parentid','0');
		$navs = $this->findAll($criteria);
		$dataArray[0] = '一级菜单';
		$navs = simplifyData($navs);
		foreach ($navs as $arr){
			$dataArray[$arr['zna_id']] = $arr['zna_title'];
		}
		return $dataArray;
	}
	/*
	 * 子菜单下垃框列表数据
	* */
	public function getChildData($parentid){
		$criteria=new CDbCriteria;
		$criteria->compare('zna_parentid',$parentid);
		$navs = $this->findAll($criteria);
		$dataArray[0] = '请选择';
		$navs = $navs ? simplifyData($navs) : array();
		foreach ($navs as $arr){
			$dataArray[$arr['zna_id']] = $arr['zna_title'];
		}
		return $dataArray;
	}
	/*
	 * 一级列表
	 * */
	public function getTop(){
		$criteria=new CDbCriteria;
		$criteria->compare('zna_parentid','0');
		if(F::sitetype()==2) $criteria->compare('zna_type',array(2,3));
		//$top = $this->findAll($criteria);
		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
			'pagination'=>array('pageSize'=>10000)
		));
	}
	/*
	 * 子列表
	* */
	public function getChild($id){
		$criteria=new CDbCriteria;
		$criteria->compare('zna_parentid',$id);
		//$Child= $this->findAll($criteria);
		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
			'pagination'=>array('pageSize'=>100)
		));
	}
	/*
	 * 加载菜单，获取一级菜单
	 * @param $type类型：1商家中心，2会员中心，3前端
	 * @param $actionId 当前控制器的ID
	 * */
	public function getTopMenu($type,$actionId){
		//获取一级菜单列表
		$criteria=new CDbCriteria;
		$criteria->compare('zna_type',$type);
		$criteria->order = 'zna_listorder ASC';
		$criteria->compare('zna_parentid','0');
		$dataMenu = $this->findAll($criteria);
		//获取活动的一级菜单
		$criteria = new CDbCriteria;
		$criteria->compare('zna_type',$type);
		$criteria->compare('zna_topid','>0');
		$modulename = ($type==1)?'/company/':'/member/';
		$criteria->compare('zna_url',$modulename.$actionId,true);
		$activeTop = $this->find($criteria);
		if(!empty($activeTop)){
			$activeTop = $activeTop->zna_topid;
		}else{
			throw new CHttpException(404,'目录加载错误！'); 
		}
		//遍历结果生成菜单
		if(!empty($dataMenu)){
			$dataMenu = simplifyData($dataMenu);
			foreach($dataMenu as $arr){
				$menuArray[] = array('label'=>$arr['zna_title'],
						'url'=>$arr['zna_url'],
						'active'=>($arr['zna_id']==$activeTop),
				);
			}
			return $menuArray;
		}else{
			throw new CHttpException(404,'目录加载错误！');
		}
	}
	/**
	 * 加载菜单，获取二级三级菜单列表
	 * @param $type类型：1商家中心，2会员中心，3前端
	 * @param $actionId 当前控制器的ID
	 * */
	public function getMenu($type,$actionId){
		//获取活动的一级菜单
		$criteria = new CDbCriteria;
		$criteria->compare('zna_type',$type);
		$criteria->compare('zna_topid','>0');
		$modulename = ($type==1)?'/company/':'/member/';
		$criteria1 = clone $criteria;
		$criteria1->compare('zna_url',$modulename.Yii::app()->getController()->id,true);
		//如果一个控制器下存在多个菜单，则需细分
		if($this->count($criteria1)>1){
			$criteria2 = clone $criteria;
			$criteria2->compare('zna_url',$modulename.Yii::app()->getController()->id.'/'.Yii::app()->getController()->active_action,true);
			$activeTop =  $this->find($criteria2);
			if(empty($activeTop))
				$activeTop =  $this->find($criteria1);
		}else{
			$activeTop =  $this->find($criteria1);
		}
		if(!empty($activeTop)){
			$activeTopId = $activeTop->zna_topid;
		}else{
			throw new CHttpException(404,'目录加载错误！'); 
		}
		//获取对应的二级菜单
		$criteria = new CDbCriteria;
		$criteria->compare('zna_type',$type);
		$criteria->compare('zna_parentid',$activeTopId);
		$criteria->order = 'zna_listorder ASC';
		$navSed = $this->findAll($criteria);
		if(!empty($navSed)){
			$navSed = simplifyData($navSed);
			foreach ($navSed as $keySed=>$arrSed){
				//二级菜单状态不为显示状态，隐藏标题
				if($arrSed['zna_status']==1){
					$menu[$keySed]['title'] = $arrSed['zna_title'];
				}else{
					$menu[$keySed]['title'] = '';
				}
				//加载二级菜单下的三级菜单
				$criteria = new CDbCriteria;
				$criteria->compare('zna_parentid',$arrSed['zna_id']);
				$criteria->compare('zna_status','1');//仅展示显示状态的菜单
				$criteria->order = 'zna_listorder ASC';
				$navThr = $this->findAll($criteria);
				if(!empty($navThr)){
					$navThr = simplifyData($navThr);
					foreach ($navThr as $arrThr){
						$menu[$keySed]['items'][]=array(
												'label'=>$arrThr['zna_title'],
												'url'=>$arrThr['zna_url'],
												'active'=>($arrThr['zna_id']==$activeTop->zna_id),
													);
					}
				}
			}
			//print_r($menu);exit;
			return $menu;
		}else{
			throw new CHttpException(404,'二级目录加载错误！');
		}
	}
	
}